package com.example.shirodemo.controller;

import com.example.shirodemo.pojo.SysRole;
import com.example.shirodemo.pojo.SysUser;
import com.example.shirodemo.repository.SysRoleRepository;
import com.example.shirodemo.repository.SysUserRepository;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class TestController {
    @Autowired
    private SysUserRepository sysUserRepository;
     @Autowired
    private SysRoleRepository sysRoleDao;
     /**
      * 用户查询.
      *
      * @return
      */
     @RequestMapping("/list")
     @RequiresPermissions(value = "admin:view")//权限管理;
     public ModelAndView userInfo(@RequestParam(value = "start", defaultValue = "0") Integer start, @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        System.out.println("admin/list********************* ***");
        start = start < 0 ? 0 : start;
        Sort sort = Sort.by(Sort.Order.desc("uid"));
        Pageable pageable = PageRequest.of(start, limit, sort);
        Page<SysUser> page = sysUserRepository.findAll(pageable);
        //session 由controller 注入参数传入
        SysUser adminprincipal = (SysUser) SecurityUtils.getSubject().getPrincipal();
       //String loginName = (String) SecurityUtils.getSubject().getPrincipal();
       ModelAndView modelAndView = new ModelAndView("list");
        modelAndView.addObject("page", page);
        modelAndView.addObject("name", adminprincipal.getName());
        modelAndView.addObject("Username", adminprincipal.getUsername());
        modelAndView.addObject("RoleList", adminprincipal.getRoles());
        return modelAndView;
  }
     /**
      * 用户添加
      */
     @RequestMapping("/addtest")
      /**
      * 权限管理
      */
      @RequiresPermissions("admin:add")
      public String adminAdd(String name, String password, String role) {
       SysUser admin = new SysUser();
                 /**
                 * 加密的次数
                 */
          int hashIterations = 2;
                /**
                 * 盐值这里的salt是 username+salt（一般是用户名加
         一个随机字符串）, 这里以字符串“long”为例)
                 */
           Object salt = name+"yan";
                /**
                 * 密码
                 */
            Object credentials = "123456";
                 /**
                 * 加密方式
                 */
            String hashAlgorithmName = "MD5";
            Object simpleHash = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
            admin.setUsername(name);
            admin.setPassword(simpleHash.toString());
            admin.setSalt("yan");
            List<SysRole> roles = new ArrayList<>();
            SysRole role1 = sysRoleDao.findByRole("admin");
            roles.add(role1);
            admin.setRoles(roles);
            sysUserRepository.save(admin);
            return "Add";
  }
       /**
      * 用户添加
      */
       @RequestMapping("/add")
       @RequiresPermissions("admin:add")
       public String adminAdd() {
          return "Add";
      }
      /**
      * 用户删除;
      */
      @RequestMapping("/del")
      @RequiresPermissions("admin:del")//权限管理;
      public String adminDel() {
         return "Del";
  }
}